package com.zxl.live.screen.call.helper;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ProgressBar;

import com.zxl.live.screen.call.ui.widget.InCallActionView;
import com.zxl.live.screen.call.ui.widget.ThemePreviewWindow;
import com.zxl.live.tools.tools.DrawUtils;

/**
 * Created by Administrator on 2017/12/10.
 */

public class PreviewAnimHelper {

    private float btnTranslationY;

    private OvershootInterpolator mOvershootInterpolator;

    public PreviewAnimHelper(float btnTranslationY) {
        this.btnTranslationY = btnTranslationY;
        mOvershootInterpolator = new OvershootInterpolator(1.5f);
    }

    public void showApplyBtn(Button applyBtn) {
        applyBtn.animate().translationY(0.0F).setDuration(400L).setInterpolator(mOvershootInterpolator).setStartDelay(500).start();
    }

    public void dismissApplyBtn(Button applyBtn) {
        applyBtn.animate().translationY(btnTranslationY).setDuration(400L).setInterpolator(mOvershootInterpolator).setStartDelay(0L).start();
    }

    public void startAnim(ThemePreviewWindow previewWindow, InCallActionView actionView) {
        final View[] arrayOfView = new View[4];
        arrayOfView[0] = previewWindow.getmIconView();
        arrayOfView[1] = previewWindow.getmNameView();
        arrayOfView[2] = previewWindow.getmNumberView();
        arrayOfView[3] = actionView;
        int i1 = DrawUtils.getScreenHeight();
        int[] arrayOfInt = {0, 0, 0, 0};
        final float[] arrayOfFloat = new float[4];
        arrayOfFloat[0] = (0.15F * -i1);
        arrayOfFloat[1] = (0.12F * -i1);
        arrayOfFloat[2] = (0.12F * -i1);
        arrayOfFloat[3] = (0.15F * i1);
        final DecelerateInterpolator localDecelerateInterpolator = new DecelerateInterpolator(2.0F);
        for (int i2 = 0; i2 < arrayOfView.length; i2++) {
            View localView4 = arrayOfView[i2];
            localView4.setLayerType(2, null);
            localView4.setAlpha(arrayOfInt[i2]);
            localView4.setTranslationY(arrayOfFloat[i2]);
        }
        ValueAnimator t = ValueAnimator.ofFloat(new float[]{0.0F, 1.0F});
        t.setDuration(800);
        t.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator paramAnonymousValueAnimator) {
                float f1 = paramAnonymousValueAnimator.getAnimatedFraction();
                float f2 = localDecelerateInterpolator.getInterpolation(f1);
                for (int i = 0; i < arrayOfView.length; i++) {
                    View localView = arrayOfView[i];
                    localView.setAlpha(f1);
                    localView.setTranslationY(arrayOfFloat[i] * (1.0F - f2));
                }
            }
        });
        t.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator paramAnonymousAnimator) {
                super.onAnimationEnd(paramAnonymousAnimator);
                int i = arrayOfView.length;
                for (int j = 0; j < i; j++) {
                    arrayOfView[j].setLayerType(0, null);
                }
            }
        });
        t.setStartDelay(400L);
        t.start();
    }

    public void showProgress(ProgressBar mProgressBar, View mProgressTextLayout) {
        DecelerateInterpolator localDecelerateInterpolator = new DecelerateInterpolator(1.5F);
        mProgressBar.setTranslationY(btnTranslationY);
        mProgressBar.animate().alpha(1.0F).translationY(0.0F).setDuration(400).setInterpolator(localDecelerateInterpolator).start();

        mProgressTextLayout.setTranslationY(btnTranslationY);
        mProgressTextLayout.animate().alpha(1.0F).translationY(0.0F).setDuration(400).setInterpolator(localDecelerateInterpolator).start();
    }

    public void dismissProgress(ProgressBar mProgressBar, View mProgressTextLayout) {
        mProgressBar.animate().alpha(0.0F).setDuration(300L).start();
        mProgressTextLayout.animate().alpha(0.0F).setDuration(300L).start();
    }


}
